local skynet = require "skynet"
local filelog = require "filelog"
local msghelper = require "roomsvrhelper"
local msgproxy = require "msgproxy"
local base = require "base"
local configdao = require "configdao"
local json = require "cjson"
local timetool = require "timetool"
json.encode_sparse_array(true,1,1)
require "enum"

local filename = "RoomsvrRequest.lua"
local RoomsvrRequest = {}

function RoomsvrRequest.process(session, source, event, ...)
	local f = RoomsvrRequest[event] 
	if f == nil then
		filelog.sys_error(filename.." RoomsvrRequest.process invalid event:"..event)
		base.skynet_retpack(nil)
		return
	end
	f(session, source, ...)
end

--[[
//请求创建朋友桌
message CreateFriendTableReq {
	optional Version version = 1;
	optional int32   gameid = 2;
	optional PhzCreateTableCfg phzgamecfg = 3;
}

//响应创建朋友桌
message CreateFriendTableRes {
	optional int32  errcode = 1; //错误原因 0表示成功
	optional string errcodedes = 2; //错误描述
	optional string create_table_id = 3; //朋友桌索引号
	optional int32  gameid = 4;  //游戏类型id 
}
]]

function RoomsvrRequest.createfriendtable(session, source, request)
	local responsemsg = {
		errcode = EErrCode.ERR_SUCCESS,
	}

	if request == nil or request.gameid == nil or request.nngamecfg == nil then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "无效请求！"
		base.skynet_retpack(responsemsg)
		filelog.sys_error("RoomsvrRequest.createfriendtable two invalid request param")
		return
	end

	local conf = {
		conf_version = 1,
		gameid = request.gameid,
		name = request.name or request.playerinfo.rolename.."的房间",
	    create_user_rid = request.rid,
	    create_user_rolename = request.playerinfo.rolename,
	    create_user_logo = request.playerinfo.logo,
	    create_time = timetool.get_time(),
	   	action_timeout = 20,    --玩家出牌时间
		action_timeout_count = 2,
    	iscontrol = request.iscontrol,
    	max_watch_playernum = 300, --最大旁观人数
    	gemstonenum = request.gemstonenum,
	}

	if request.gameid == EGameId.GAME_ID_NIUNIU then
		conf.retain_to_num = request.nngamecfg.retain_to_num
		conf.pattern = request.nngamecfg.pattern
		conf.zuozhuang = request.nngamecfg.zuozhuang
		conf.room_type = conf.zuozhuang.key
		conf.game_type = ENNGameType.NNGAME_TYPE_COMMON
		conf.base_score = 1  --基础分
		conf.max_player_num = 5		---玩家数
		conf.nn_qiangzhuang_time = configdao.get_businessconfitem_by_index(100, 1000, "globalvarcfg", "nn_qiangzhuang_time")
		conf.nn_jiaofen_time = configdao.get_businessconfitem_by_index(100, 1000, "globalvarcfg", "nn_jiaofen_time")
		conf.nn_pingniu_time = configdao.get_businessconfitem_by_index(100, 1000, "globalvarcfg", "nn_pingniu_time")
	elseif request.gameid == EGameId.GAME_ID_PENGHUZI then
	    conf.max_player_num = 4		---玩家数
    	conf.retain_to_num = request.phzgamecfg.retain_to_num --碰胡子游戏局数
    	conf.playmechanism = request.phzgamecfg.playmechanism --玩法机制
    	conf.isforcehu = request.phzgamecfg.isforcehu --是否强制胡牌
    	conf.room_type = EPHZRoomType.PHZROOM_TYPE_FRIEND_COMMON
    	conf.game_type = EPHZGameType.PHZGAME_TYPE_COMMON
	end

    local result, create_table_id = msghelper:create_friend_table(conf)
    if not result then
		responsemsg.errcode = EErrCode.ERR_CREATE_TABLE_FAILED
    	responsemsg.errcodedes = "系统错误，创建朋友桌失败！"
		filelog.sys_error("RoomsvrRequest.createfriendtable create_friend_table failed")
    else
    	responsemsg.create_table_id = create_table_id
    end

    base.skynet_retpack(responsemsg)
end

function RoomsvrRequest.gmcmd(cmd, ... )
	if cmd == "reload" then
		msghelper:event_process("lua", "cmd", "reload", ...)
		return
	end
end

return RoomsvrRequest